export function is(val: unknown, type: string) {
  return Object.prototype.toString.call(val) === `[object ${type}]`;
}

export function isNull(val: unknown): val is null {
  return val === null;
}

export function isObject(val: unknown): val is Record<any, any> {
  return !!val && is(val, 'Object');
}

export function isString(val: unknown): val is string {
  return is(val, 'String');
}

export function isArray(val: unknown): val is Array<any> {
  return !!val && Array.isArray(val);
}

export function isFunction(val: unknown): boolean {
  return !!val && (typeof val === 'function');
}

export function isPromise<T = any>(val: unknown): val is Promise<T> {
  return is(val, 'Promise') && isObject(val) && isFunction(val.then) && isFunction(val.catch);
}

export function isEmpty<T = unknown>(val: T): val is T {
  if (isArray(val) || isString(val)) return val.length === 0;
  if (val instanceof Map || val instanceof Set) return val.size === 0;
  return isObject(val) && Object.keys(val).length === 0;
}